﻿using System;
using System.Collections.Generic;
using System.Data;
using ContractManager.Common.Class;
using ContractManager.Entity;
using Speed.Data;

namespace ContractManager.BLL.Business
{
    public static class ClsRevenues
    {
        public static List<Revenues> SelectAll()
        {
            using (var db = Sys.NewDb())
            {
                return Data.Base.Revenues.Select(db);
            }
            
        }

        public static DataTable GetAll(int formNo, int voucherCate, DateTime fromDate, DateTime toDate)
        {
            using (var db = Sys.NewDb())
            {
                string commandText = String.Format("EXEC dbo.usp_Revenues_GetAll {0},{1},{2},{3},'{4}','{5}'",
                                            ClsVariable.AsSysMyCoYearId,
                                            formNo,
                                            voucherCate,
                                            ClsVariable.EmployeeId,
                                            fromDate.ToString("dd/MMM/yyyy"),
                                            toDate.ToString("dd/MMM/yyyy"));
                return db.ExecuteDataTable(commandText);
            }

        }

        public static DataTable ListDetail(Int32 revId)
        {
            using (var db = Sys.NewDb())
            {
                string commandText = String.Format("EXEC dbo.usp_RevenueDetails_SelectByMaster {0}", revId);
                return db.ExecuteDataTable(commandText);
            }
            
        }

        public static void Insert(Revenues capital)
        {
            using (var db = Sys.NewDb())
            {
                db.BeginTransaction();
                db.Insert<Revenues>(capital, EnumSaveMode.Requery);

                foreach (var addItem in capital.ListAdd)
                {
                    addItem.RevId = capital.RevId;
                    db.Insert<RevenueDetails>(addItem);
                }
                foreach (var updateItem in capital.ListUpdate)
                {
                    updateItem.RevId = capital.RevId;
                    db.Update<RevenueDetails>(updateItem);
                }
                foreach (var delItem in capital.ListDelete)
                {
                    db.Delete<RevenueDetails>(string.Format("RevDetId={0}", delItem.RevDetId));
                }

                db.Commit();
            }
            
            //db.Insert<Revenues>(capital);
        }

        public static void Update(Revenues capital)
        {
            using (var db = Sys.NewDb())
            {
                db.BeginTransaction();
                db.Update<Revenues>(capital);
                foreach (var addItem in capital.ListAdd)
                {
                    addItem.RevId = capital.RevId;
                    db.Insert<RevenueDetails>(addItem);
                }
                foreach (var updateItem in capital.ListUpdate)
                {
                    updateItem.RevId = capital.RevId;
                    db.Update<RevenueDetails>(updateItem);
                }
                foreach (var delItem in capital.ListDelete)
                {
                    db.Delete<RevenueDetails>(string.Format("RevDetId={0}", delItem.RevDetId));
                }
                db.Commit();
            }
            

            //db.Update<Revenues>(capital);
        }

        public static Revenues SelectByPk(Int32 revId)
        {
            using (var db = Sys.NewDb())
            {
                return db.SelectSingle<Revenues>(string.Format("RevId={0}", revId));
            }
            
        }

        public static int DeleteByPk(Int32 revId)
        {
            using (var db = Sys.NewDb())
            {
                db.BeginTransaction();
                int ret = db.Delete<Revenues>(string.Format("RevId={0}", revId));
                int rowCount = db.Delete<RevenueDetails>(string.Format("RevId={0}", revId));
                db.Commit();
                return ret;
            }
            

            //return db.Delete<Revenues>(string.Format("RevId={0}", revId));
        }
    }
}